<?php
namespace Common\Model;
use Think\Auth;
use Think\Model;
//使用D方法进行菜单管理添加
class MenuModel extends Model{
	protected $table;
	protected $menutable;
	protected $pre;

	public function __construct($menutable,$pre =''){
		parent::__construct();
		$this->pre = empty($pre) ? C('DB_PREFIX') : $pre;
		$this->table = $menutable;
		$this->menutable = $this->pre.$menutable;
	}
	/**
     * 添加菜单
     * @param $data
     */
	public function addMysql($data){
		//首先声明一个变量 来检查传过来的数组信息验证判断页面传过来的数据是否为空用函数checkMenuInfo()进行封装
		$checkInfo = $this -> checkMenuInfo($data);
		if($checkInfo !== true){
			return $checkInfo;//如果没有验证成功就返回去给控制器的$this -> ajaxReturn()
		}
		$menuData = $this -> formatMenuInfo($data);//验证通过将传过来的数据进行格式化整理开始准备向数据库添加
		// dump($menuData);exit; 
		$menu = M($this->table);
		$mid = $menu -> add($menuData,[],true);
	    // echo $mid;exit;
		if(!$mid){
			return $this -> returnInfo('','很遗憾，继续加油！');
		}
		return $this -> returnInfo('','恭喜你，再接再厉',1);

	}
	//对前台传过来的数据进行验证
	public function checkMenuInfo($data){
		if(empty($data['title'])){
			return $this -> returnInfo('','菜单名称不能为空！');
		}
		if(empty($data['link'])){
			return $this -> returnInfo('','链接名称不能为空！');
		}
		if(!empty($data['sort']) && (strlen($data['sort']) > 6)){
			return $this -> returnInfo('','排序不符合规则！');
		}
		$menu = M($this->table);
		//验证之后看数据库中有重复的没有,如果id为空就是添加
		if(empty($data['id'])){
			$menuCont = $menu -> field('id') -> where(['title' => $data['title'],'link' => $data['link']]) -> count();
			if($menuCont > 0){
				return $this -> returnInfo('','亲，菜单已经存在！');
			}
		}
		return true;//返回值
	}

	//验证通过后对数据进行格式化
	public function formatMenuInfo($data){
		$id = empty($data['id']) ? 0 : $data['id'];
		$pid = empty($data['pid']) ? 0 : $data['pid'];
		$sort = empty($data['sort']) ? 0 :$data['sort'];
		$status = empty($data['status']) ? 0 : $data['status'];
		$type = empty($data['type']) ? 1 : $data['type'];
		$dataInfo = ['id'=>$id,'title'=>$data['title'],'link'=>$data['link'],'sort'=>$sort,'pid'=>$pid,'type'=> $type, 'status' => $status];
		//dump($dataInfo);exit;
		return $dataInfo;
	}


	public function returnInfo($data,$msg = '',$status = 0){
		return ['status' => $status, 'mess' => $msg,'data' => $data];
	}

	public function Menulist($id,$field = 'id',$order = 'sort desc'){
		$menu = M($this->table);
		$list = $menu -> field($field);
		//如果有id的话通过id来展示进行拼接sql语句
		if(!empty($id)){
			$where['id'] = $id;//$where变成一个数组where(['id' => $id])
			$list = $list -> where($where);
		}
		$list = $list-> order($order) -> select();
		if(empty($list)){
			return $this -> returnInfo('','没有数据！',0);
		}else{
			return $this -> returnInfo($list,'成功找到',1);
		}
	}

	public function Alterstate($id){
		if(empty($id) || (1 == $id)){
			return $this->returnInfo('','修改异常');
		}
		$menu = M($this ->table);
		$menuInfo = $menu -> field('status') -> where(['id' => $id]) -> find();
		$result = [];
		if($menuInfo['status'] == 0){
			$menu -> where(['id' => $id]) -> setField(['status' => 1]);
			$result = $this->returnInfo(1,'已开启',1);
		}else{
			$menu -> where(['id' => $id]) -> setField(['status' => 0]);
			$result = $this->returnInfo(0,'已关闭',1);
		}
		return $result;
	}
	public function delMenu($id){
		if(empty($id) || (1 == $id)){
			$this -> returnInfo('','操作异常！');
		}
		$is_del = M($this->table) -> where(['id' => $id]) ->delete();
		if(!$is_del){
			return $this -> returnInfo('','删除失败！');
		}
		return $this -> returnInfo('','删除成功！',1);
	}

	public function getNavlist(){
		$data = M($this->table) -> field('id,pid,title,link') -> select();
		$auth = new \Think\Auth();//开启权限
		if(session('user.uid') == 1){
			 return makeTree($data);           
		} 
		foreach ($data as $key => $val) {
			if(!$auth -> check($val['link'],session('user.uid'))){
				unset($data[$key]);
			}
		}
		return makeTree($data);
	}


}


